package com.ruoyi.iot.util;

import cn.hutool.core.date.DatePattern;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.ruoyi.iot.domain.data.FieldRunData;
import org.springframework.security.core.parameters.P;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**南土所数据读取*/
public class NtsHttpUtils {
    public static List<FieldRunData> getData(String url, String deviceCode, String token){
        String getUrl=url+token+"/"+deviceCode+"/snapshot";
//        HashMap<String,Object> kv=new HashMap<String, Object>();
//        kv.put("accessToken",token);
        JSONObject lastData=HttpUtils.getUrlHttpDataUrlParam(getUrl,null);
        Boolean statueCode=lastData.getBool("success");
        List<FieldRunData> list=new ArrayList<FieldRunData>();
        if(statueCode){
            JSONObject result= lastData.getJSONObject("data");
            String sType=result.getStr("stype");
            if(sType.equals("TR1")){//土壤传感器
                String monitorTime=result.getStr("at");
                String vals=result.getStr("vals");
                String keys="vmc,wd,jdcs,bgec,gxsec";
                String[] valArray=vals.split(",");
                String[] keyArray=keys.split(",");
               for(int i=0;i<valArray.length ;i+=1) {
                   FieldRunData rData=new FieldRunData();
                   rData.setMonitoringTime(LocalDateTime.parse(monitorTime, DatePattern.NORM_DATETIME_FORMATTER));
                   rData.setField(keyArray[i]);
                   rData.setFieldValue(valArray[i]);
                   list.add(rData);
               }
            }
        }
        return list;
    }
}
